﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using TaobaoDistrSales.Model;

namespace TaobaoDistrSales.Common
{
    public class Logger
    {
        public static void Write(string module,LogType type,string message,string traceTrack, short shopID)
        { 
            using (taobaodbEntities entities = ResourceManager.LoadEntity())
            {
                var shop = (from s in entities.Shop
                            where s.ShopId == shopID
                            select s).FirstOrDefault();

                message = message.Length > 500 ? message.Substring(0, 500) : message;
                traceTrack = traceTrack.Length > 1900 ? traceTrack.Substring(0, 1900) : traceTrack;
                Model.Logs logs = new Model.Logs();
                logs.message = message;
                logs.module = module;
                logs.tracestack = traceTrack;
                logs.type = type.ToString();
                logs.Shop = shop;
                logs.created = DateTime.Now;
                entities.AddToLogs(logs);
                entities.SaveChanges();
            } 
        }

        public static void WriteLogToFile(string fileName, string logContent)
        {
            using (StreamWriter sWriter = new StreamWriter(fileName,false))
            {
                sWriter.Write(logContent);
                sWriter.Close();
            }
        }
    }

    public enum LogType
    { 
        Normal,

        Exception,

        Error
    }
}
